﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using SeguradoraIA.Framework.Configuration;

namespace SeguradoraIA.Framework.Log
{
    public static class LogManager
    {
        private static readonly List<ILogger> loggers = new List<ILogger>();
        private static readonly bool logMessage;
        private static readonly bool logReturn;
        private static readonly bool logSQL;
        private static readonly bool logEntry;
        private static readonly bool logException;


        static LogManager()
        {
            try
            {
                Configurator.GetInstance().GetLoggers(loggers);
                Configurator.GetInstance().GetLoggerPublishingStatus(ref logMessage, ref logReturn, ref logSQL, ref logEntry, ref logException);
            }
            catch (Exception ex)
            {
                throw new Exception("LogManager - Falha ao configurar Log", ex);
            }

        }

        private static bool IsLogTurnedOn()
        {
            return (loggers.Count > 0);
        }

        private static void DoLog(LogData ld)
        {
            foreach (ILogger logger in loggers)
            {
                logger.Log(ld);
            }
        }

        public static void Log(string mensagem)
        {
            if (!IsLogTurnedOn() || !logMessage)
                return;

            DoLog(new SimpleLogData(mensagem, null));
        }

        public static void Log(string mensagem, params object[] parametros)
        {
            if (!IsLogTurnedOn() || !logMessage)
                return;

            DoLog(new SimpleLogData(mensagem, parametros));
        }

        public static void LogReturn(object obj)
        {
            if (!IsLogTurnedOn() || !logReturn)
                return;

            DoLog(new ReturnLogData(obj));
        }

        public static void LogException(Exception ex)
        {
            LogException(ex, null);
        }

        public static void LogException(Exception ex, string mensagemAdicional)
        {
            if (!IsLogTurnedOn() || !logException)
                return;

            DoLog(new ExceptionLogData(ex, mensagemAdicional));
        }

        public static void LogEntryParameters(params object[] parametros)
        {
            if (!IsLogTurnedOn() || !logEntry)
                return;

            DoLog(new EntryLogData(parametros));
        }

        public static void LogSQL(string statement, List<SqlParameter> parameters)
        {
            if (!IsLogTurnedOn() || !logSQL)
                return;

            DoLog(new SQLLogData(statement, parameters));
        }

        public static void LogSQL(string statement)
        {
            if (!IsLogTurnedOn() || !logSQL)
                return;

            DoLog(new SQLLogData(statement, null));
        }


        public static void LogReturn()
        {
            throw new NotImplementedException();
        }
    }
}